﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;

namespace l1cs.server.serverpackets
{
    public class S_SkillBuy : ServerBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( S_SkillBuy ).FullName );

        private byte[] _byte = null;

        public S_SkillBuy( int o, L1PcInstance pc )
        {
            int count = Scount( pc );
            int inCount = 0;
            for ( int k = 0; k < count; k++ )
            {
                if ( !pc.isSkillMastery( ( k + 1 ) ) )
                {
                    inCount++;
                }
            }
            writeC( Opcodes.S_OPCODE_SKILLBUY );
            writeD( 100 );
            writeH( inCount );
            for ( int k = 0; k < count; k++ )
            {
                if ( !pc.isSkillMastery( ( k + 1 ) ) )
                {
                    writeD( k );
                }
            }
        }

        public int Scount( L1PcInstance pc )
        {
            int RC = 0;
            switch ( pc.getType() )
            {
                case 0: // 君主
                    if ( pc.getLevel() > 20 || pc.isGm() )
                    {
                        RC = 16;
                    }
                    else if ( pc.getLevel() > 10 )
                    {
                        RC = 8;
                    }
                    break;

                case 1: // ナイト
                    if ( pc.getLevel() >= 50 || pc.isGm() )
                    {
                        RC = 8;
                    }
                    break;

                case 2: // エルフ
                    if ( pc.getLevel() >= 24 || pc.isGm() )
                    {
                        RC = 23;
                    }
                    else if ( pc.getLevel() >= 16 )
                    {
                        RC = 16;
                    }
                    else if ( pc.getLevel() >= 8 )
                    {
                        RC = 8;
                    }
                    break;

                case 3: // WIZ
                    if ( pc.getLevel() >= 12 || pc.isGm() )
                    {
                        RC = 23;
                    }
                    else if ( pc.getLevel() >= 8 )
                    {
                        RC = 16;
                    }
                    else if ( pc.getLevel() >= 4 )
                    {
                        RC = 8;
                    }
                    break;

                case 4: // DE
                    if ( pc.getLevel() >= 24 || pc.isGm() )
                    {
                        RC = 16;
                    }
                    else if ( pc.getLevel() >= 12 )
                    {
                        RC = 8;
                    }
                    break;

                default:
                    break;
            }
            return RC;
        }

        override
        public byte[] getContent()
        {
            if ( _byte == null )
            {
                _byte = _bao.ToArray();
            }
            return _byte;
        }

        override
        public String getType()
        {
            return "[S] S_SkillBuy";
        }
    }
}